using System.Collections.Generic;
using System.Linq;
using TeachingManagementSystem.DAL;
using TeachingManagementSystem.Models;
using System;

namespace TeachingManagementSystem.BLL
{
    public class ClassroomBLL
    {
        private DataContext _context;

        public ClassroomBLL()
        {
            _context = new DataContext();
        }

        // 获取所有教室
        public List<Classroom> GetAllClassrooms()
        {
            return _context.Classrooms?.ToList() ?? new List<Classroom>();
        }

        // 根据ID获取教室
        public Classroom GetClassroomByID(int classroomID)
        {
            if (classroomID <= 0)
                return null;
            return _context.Classrooms?.FirstOrDefault(c => c.ClassroomID == classroomID);
        }

        // 获取可用教室
        public List<Classroom> GetAvailableClassrooms()
        {
            return _context.Classrooms?.Where(c => c.IsAvailable).ToList() ?? new List<Classroom>();
        }

        // 根据类型获取教室
        public List<Classroom> GetClassroomsByType(ClassroomType type)
        {
            return _context.Classrooms?.Where(c => c.Type == type).ToList() ?? new List<Classroom>();
        }

        // 添加教室
        public void AddClassroom(Classroom classroom)
        {
            if (classroom == null)
                return;
                
            // EF Core会自动处理ID自增
            _context.Classrooms.Add(classroom);
            _context.SaveChanges(); // 保存到数据库
        }

        // 更新教室
        public void UpdateClassroom(Classroom classroom)
        {
            if (classroom == null || classroom.ClassroomID <= 0)
                return;
                
            var existingClassroom = _context.Classrooms?.FirstOrDefault(c => c.ClassroomID == classroom.ClassroomID);
            if (existingClassroom != null)
            {
                existingClassroom.ClassroomNumber = classroom.ClassroomNumber;
                existingClassroom.Building = classroom.Building;
                existingClassroom.Capacity = classroom.Capacity;
                existingClassroom.Type = classroom.Type;
                existingClassroom.Equipment = classroom.Equipment;
                existingClassroom.IsAvailable = classroom.IsAvailable;
                
                _context.SaveChanges(); // 保存到数据库
            }
        }

        // 删除教室
        public void DeleteClassroom(int classroomID)
        {
            if (classroomID <= 0)
                return;
                
            var classroom = _context.Classrooms?.FirstOrDefault(c => c.ClassroomID == classroomID);
            if (classroom != null && _context.Classrooms != null)
            {
                _context.Classrooms.Remove(classroom);
                _context.SaveChanges(); // 保存到数据库
            }
        }

        // 检查教室容量是否满足需求
        public bool CheckCapacity(int classroomID, int requiredCapacity)
        {
            if (classroomID <= 0 || requiredCapacity <= 0)
                return false;
                
            var classroom = _context.Classrooms?.FirstOrDefault(c => c.ClassroomID == classroomID);
            return classroom != null && classroom.Capacity >= requiredCapacity;
        }
    }
}